package com.kool.kadmin.common.constant;

import lombok.Getter;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Getter
public enum ErrorCode {
    SUCCESS(0, "成功"),
    SYSTEM_ERROR(1001, "系统异常，请联系管理员！异常码：%s"),
    REQ_PARAM_NULL(1002, "请求参数为空"),
    CONFIG_ERROR(1003, "配置信息错误:%s"),
    REQ_PARAM_ERROR(1004, "请求参数错误:%s"),
    SELECT_NULL(1005, "查询结果为空:%s"),
    CONCURRENT_ERROR(1006, "请求重复提交:%s"),
    BUSINESS_ERROR(1007, "%s"),
    NOTIFY_ERROR(1008, "回调错误:%s"),
    TOKEN_ERROR(1009, "登录信息过期，请重新登录"),
    ZK_ERROR(1010, "Zookeeper错误:%s"),
    HTTP_ERROR(1011, "发起http请求错误，%s"),
    DUPLICATE_KEY_ERROR(1012, "插入数据重复，请检查唯一性，%s"),
    TASK_ERROR(1013, "定时任务【%s】发生异常，%s"),
    ES_ERROR(1014, "ES异常，%s"),
    CLIENT_ERROR(1015, "调用外部服务【%s】发生异常，%s");

    Integer code;
    String desc;

    ErrorCode(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static Map<Integer, ErrorCode> codeToEnum = Stream.of(ErrorCode.values()).collect(Collectors.toMap(ErrorCode::getCode, self -> self));

    public static ErrorCode getEnumByCode(Integer errorCode) {
        return codeToEnum.get(errorCode);
    }

    public static String getMsgByCode(Integer errorCode) {
        return codeToEnum.get(errorCode) == null ? null : codeToEnum.get(errorCode).getDesc();
    }
}
